﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MyCommerce.ManagementBase.Common.Base.Common;
using MyCommerce.Presentation.GeneralParameters.IViews;
using MyCommerce.Presentation.GeneralParameters.Presenters;
using Innova.Microfinanzas.BusinessEntities;

namespace MyCommerce.Windows.GeneralParameters.Views
{
    public partial class CommonClassifierView : BaseForm, ICommonClassifier
    {
        private CommonClassifierPresenter vPresenter;
        private bool isUpdate = false;

        public CommonClassifierView()
        {
            InitializeComponent();
            vPresenter = new CommonClassifierPresenter((ICommonClassifier)this);
        }

        #region ICommonClassifier Members

        private Classifier vobjClassifier;
        public Classifier objClassifier
        {
            get
            {
                vobjClassifier = new Classifier();
                if (isUpdate)
                    vobjClassifier.IdClassifier = (long)grvData.ActiveRow.Cells["IdClassifier"].Value;
                vobjClassifier.Name = txtName.Text;
                vobjClassifier.Code = txtCode.Text;
                vobjClassifier.IsActive = checkIsActive.Checked;
                vobjClassifier.OrderBy = 1;
                vobjClassifier.IdClassifierType = (long)cmbClassifierType.SelectedValue;
                return vobjClassifier;
            }
            set
            {
                txtName.Text = value.Name;
                txtCode.Text = value.Code;
                checkIsActive.Checked = value.IsActive;
                if (String.IsNullOrEmpty(cmbClassifierType.ValueMember) == false)
                    cmbClassifierType.SelectedValue = value.IdClassifierType;
            }
        }

        public List<Classifier> colClassifier
        {
            set
            {
                //grvData.DataSource = value;
            }
        }

        public List<ClassifierType> colClassifierType
        {
            set
            {
                grvData.DataSource = value;
                cmbClassifierType.DataSource = value;
                cmbClassifierType.DisplayMember = "Name";
                cmbClassifierType.ValueMember = "IdClassifierType";
            }
        }

        public new event EventHandler OnLoad;

        public event OnEntityEventHandler OnEdit;

        public event OnEntityEventHandler OnDelete;

        public event OnSaveEventHandler OnSave;

        #endregion
                
        /// <summary>
        /// Cargamos la pantalla.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UserAdministrationView_Load(object sender, EventArgs e)
        {
            OnLoad(sender, e);
            Modify(false);
        }

        /// <summary>
        /// Cerramos la pantalla.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsCloseButtonToolStrip_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        
        private void tsNewButtonToolStrip_Click(object sender, EventArgs e)
        {
            commonButtonsToolStrip1.ToggleButtons();
            Modify(true);
        }

        private void tsEditButtonToolStrip_Click(object sender, EventArgs e)
        {
            if (grvData.ActiveRow.Band.Index == 0)
            {
                Context.ShowInformationMessageBox("Debe seleccionar un nodo hijo.", "Validación de datos.");
                return;
            }
            else
            {
                if (grvData.ActiveRow.Selected)
                {
                    OnEdit(sender, (long)grvData.ActiveRow.Cells["IdClassifier"].Value);
                    isUpdate = true;
                }
                else
                {
                    Context.ShowInformationMessageBox("Debe seleccionar un nodo hijo.", "Validación de datos.");
                }
            }
            commonButtonsToolStrip1.ToggleButtons();
            Modify(true);
        }

        private void tsSaveButtonToolStrip_Click(object sender, EventArgs e)
        {
            try
            {
                if (!DataAutoValidate())
                    return;

                if (!isUpdate)
                {
                    if (OnSave != null)
                    {
                        OnSave(this, true);
                        Context.ShowInformationMessageBox("La información se guardó correctamente.", "Creación/Edición de parametros.");
                    }

                }
                else
                {
                    if (OnSave != null)
                    {
                        OnSave(this, false);
                        Context.ShowInformationMessageBox("La información se actualizó correctamente.", "Creación/Edición de parametros.");
                    }
                }
                isUpdate = false;
                commonButtonsToolStrip1.ToggleButtons();
                Modify(false);
                CleanScreen();
            }
            catch (Exception ex)
            {
                Context.HandleException(ex);
            }
            
            
        }

        private void tsCancelButtonToolStrip_Click(object sender, EventArgs e)
        {
            isUpdate = false;
            commonButtonsToolStrip1.ToggleButtons();
            Modify(false);
            CleanScreen();
        }

        private void tsDeleteButtonToolStrip_Click(object sender, EventArgs e)
        {
            try
            {
                if (grvData.ActiveRow.Band.Index == 0)
                {
                    Context.ShowInformationMessageBox("Debe seleccionar un nodo hijo.", "Validación de datos.");
                }
                else
                {
                    if (grvData.ActiveRow.Selected)
                    {
                        if (Context.ShowQuestionMessageBox("¿Desea eliminar a este parametro?", "Eliminar parametro") == System.Windows.Forms.DialogResult.Yes)
                        {
                            OnDelete(sender, (long)grvData.ActiveRow.Cells["IdClassifier"].Value);
                            Context.ShowInformationMessageBox("El parametro se eliminó correctamente.", "Eliminar parametros");
                        }
                    }
                    else
                    {
                        Context.ShowInformationMessageBox("Debe seleccionar un nodo hijo.", "Validación de datos.");
                    }
                }
            }
            catch (Exception ex)
            {
                Context.HandleException(ex);
            }            
        }

        /// <summary>
        /// Validacion de los datos.
        /// </summary>
        /// <returns></returns>
        private Boolean DataAutoValidate()
        {
            bool result = true;
            StringBuilder message = new StringBuilder();

            if (string.IsNullOrEmpty(txtName.Text.Trim()))
            {
                result = false;
                message.AppendLine("Debe ingresar un nombre.");
            }

            if (string.IsNullOrEmpty(txtCode.Text.Trim()))
            {
                result = false;
                message.AppendLine("Debe ingresar un codigo.");
            }

            if (!result)
            {
                Context.ShowErrorMessageBox(message.ToString(), "Validación de datos.");
            }

            return result;
        }

        /// <summary>
        /// Modifica el estado de edicion de la pantalla.
        /// </summary>
        /// <param name="edit"></param>
        private void Modify(bool edit)
        {
            grvData.Enabled = !edit;
            txtName.Enabled = edit;
            txtCode.Enabled = edit;
            checkIsActive.Enabled = edit;
            cmbClassifierType.Enabled = edit;

        }

        /// <summary>
        /// Limpia los campos.
        /// </summary>
        private void CleanScreen()
        {
            txtName.Text = string.Empty;
            txtCode.Text = string.Empty;
            checkIsActive.Checked = false;            
        }
    }
}
